/*
 * @name: 代理店铺管理
 * @Author: along
 * @Edition: 1.3.6
 * @Date: 2020-05-06
 */
<template>
    <div class="container goodsAgentCategory">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">店铺名称:</p>
                    <el-input
                        v-model="head.shop_title"
                        placeholder="请输入内容"
                        size="small"
                        suffix-icon="el-icon-search"
                        clearable
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">企业名称:</p>
                    <el-input
                        v-model="head.company_title"
                        placeholder="请输入内容"
                        size="small"
                        suffix-icon="el-icon-search"
                        clearable
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">是否推荐:</p>
                    <el-select
                        v-model="head.recommend_status"
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                        clearable
                    >
                        <el-option
                            v-for="item in recommend"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">是否展示:</p>
                    <el-select
                        v-model="head.cate_show"
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                        clearable
                    >
                        <el-option
                            v-for="item in cateShow"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="goodsAgentCategory"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="shop_title"
                label="店铺名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="shop_logo"
                label="店铺logo"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <img
                        v-if="scope.row.shop_logo !== ''"
                        :src="scope.row.shop_logo"
                        style="width: 40px;height:40px"
                    >
                    <p v-else>--</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="company_name"
                label="所属企业"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="agent_sort"
                label="排序"
                :formatter="emptyFormatter"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.agent_sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            style="color: #606266"
                            @click="fnEditSort(scope.row, true)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="recommend_status"
                label="是否推荐"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.recommend_status == 1">是</span>
                    <span v-if="scope.row.recommend_status == 0">否</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="recommend_sort"
                label="推荐排序"
                :formatter="emptyFormatter"
                sortable="custom"
                :width="100"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.recommend_sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            style="color: #606266"
                            @click="fnEditSort(scope.row, false)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="agent_num"
                label="代理人数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                :width="100"
            >
                <template slot-scope="scope">
                    <p
                        class="cursor-font"
                        @click="fnJumpZd(scope.row)"
                        v-if="Number(scope.row.agent_num) > 0"
                    >
                        {{ scope.row.agent_num }}
                    </p>
                    <p v-else>--</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="agent_fake_num"
                label="虚拟代理人数"
                :formatter="emptyFormatter"
                :width="130"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.agent_fake_num }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            style="color: #606266"
                            @click="fnEditFack(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="agent_show"
                label="是否展示"
                :width="120"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.agent_show"
                        :active-value="1"
                        :inactive-value="0"
                    />
                    <div
                        class="zd-fixed"
                        @click="fnSettingCoupon(scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                prop="goods_num"
                label="商品数量"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                :width="100"
            />
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <div class="zd-list">
                        <el-button
                            type="text"
                            class="cursor-font"
                            v-if="scope.row.recommend_status == 0"
                            @click="fnSetRecommend(scope.row)"
                        >
                            设为推荐
                        </el-button>
                        <el-button
                            type="text"
                            class="cursor-tip"
                            v-if="scope.row.recommend_status == 1"
                            @click="fnRelRecommend(scope.row)"
                        >
                            取消推荐
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    /**
     * @param   {String}     cate_no                记录编号，用于编辑
     * @param   {Object}     rules                  弹窗字段验证规则
     * @param   {Object}     ruleForm               弹窗字段
     * @param   {Boolean}    dialogVisible          弹窗开关
     * @param   {String}     type                   弹窗类型add:创建 edit:编辑
     * @param   {Object}     filterData             排序字段
     * @param   {Array}      recommend              推荐筛选列表
     * @param   {Array}      cateShow               展示筛选列表
     * @param   {Object}     head                   筛选字段
     * @param   {Object}     page                   分页
     * @param   {Array}      tableData              数据列表
     * @param   {OBject}     authResult             权限列表
     */
    mixins: [back],
    data () {
        return {
            filterData: {
                sort_field: 'asc',
                sorttype: 'desc'
            },
            recommend: [{
                value: '',
                label: '全部'
            },{
                value: 1,
                label: '是'
            },{
                value: 0,
                label: '否'
            }],
            cateShow: [{
                value: '',
                label: '全部'
            },{
                value: 1,
                label: '是'
            },{
                value: 0,
                label: '否'
            }],
            head: {
                shop_title: '',
                company_title: '',
                recommend_status: '',
                cate_show: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '商城'},{title: '代理店铺管理'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/shop_agent%5Cget_list', {
                page_id: page || this.page.now_page,
                keyword: this.head.shop_title,
                company_keyword: this.head.company_title,
                agent_show: this.head.cate_show,
                recommend_status: this.head.recommend_status,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 设为推荐
         */
        fnSetRecommend (row) {
            this.$post('/student/shop_agent%5Cmodify',{
                shop_no: row.shop_no,
                recommend_status: 1
            }, resp => {
                if(resp.code == 1) {
                    this.getTableData();
                    this.$message({
                        type: 'success',
                        message: '设为推荐成功',
                        center: true
                    });
                } else {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 取消推荐
         */
        fnRelRecommend (row) {
            this.$confirm('是否确认？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_agent%5Cmodify',{
                    shop_no: row.shop_no,
                    recommend_status: 0
                }, resp => {
                    if(resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '取消成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 进入掌大精英数据
         */
        fnJumpZd (row) {
            this.$router.replace({
                name: 'studentData',
                query: {
                    shop_title: row.shop_title
                }
            });
        },

        /**
         * 是否展示
         */
        fnSettingCoupon (row) {
            // console.log(row.agent_show, 'agent_show');
            if(Number(row.goods_num) == 0 && row.agent_show == 0) {
                this.$message({
                    type: 'waring',
                    message: '该店铺还未添加商品'
                });
                return;
            }

            const status = row.agent_show == 1 ? 0 : 1;

            this.$post('/student/shop_agent%5Cmodify',{
                shop_no: row.shop_no,
                agent_show: status
            },resp=>{
                if (resp.code == 1) {
                    this.$set(row, 'agent_show', status);
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 设置虚拟代理人数
         */
        fnEditFack(row) {
            this.$prompt('设置虚拟代理人数', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^(0|\+?[1-9][0-9]*)$/,
                inputPlaceholder: '请输入序号',
                inputErrorMessage: '请输入正整数',
                closeOnClickModal: false,
            }).then(({ value }) => {
                this.$post('/student/shop_agent%5Cmodify', {
                    agent_fake_num: value,
                    shop_no: row.shop_no
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '修改成功',
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 修改默认排序和推荐排序
         */
        fnEditSort(row, type) {
            if(!type && !row.recommend_status) {
                this.$message({
                    type: 'waring',
                    message: '还没有设为推荐,不允许编辑',
                    center: true
                });
                return;
            }

            this.$prompt('修改排序', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^(0|\+?[1-9][0-9]*)$/,
                inputPlaceholder: '请输入序号',
                inputErrorMessage: '请输入正整数',
                closeOnClickModal: false,
            }).then(({ value }) => {
                let data = type ? {
                    shop_no: row.shop_no,
                    agent_sort: value
                } : {
                    shop_no: row.shop_no,
                    recommend_sort: value
                };

                this.$post('/student/shop_agent%5Cmodify', data, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '修改成功',
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 数据排序
         */
        handleSortChanged(data) {
            console.log(data);
            let sortTypeMap = {
                ascending: 'asc',
                descending: 'desc'
            };

            let sortFieldMap = {
                agent_sort: 'agent_sort',
                recommend_sort: 'recommend_sort',
                agent_num: 'agent_num',
                agent_fake_num: 'agent_fake_num',
                goods_num: 'goods_num'
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .cursor-tip {
            color: rgb(228, 18, 11);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }

    }
    .zd-list {
        display: flex;
        flex-direction: column;
        align-items: center;
        .el-button {
            padding: 5px 0 !important;
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .dialog-footer {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .zd-tips {
        padding: 0 30px;
        line-height: 22px;
        text-align: center;
        margin-bottom: 30px;
        color: #FF0000;
    }
    .zd-fixed {
        width: 60px;
        height: 30px;
        position: absolute;
        left: 3px;
        top: 16px;
        z-index: 999;
        cursor: pointer;
    }
</style>
<style lang="less">
    .goodsAgentCategory{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .upload-good-image{
            border: 1px solid red;
            width: 200px;
            height: 200px;
            display: flex;
            border: 1px dashed #d9d9d9;
            border-radius: 4px;
            align-items: center;
            justify-content: center;
            font-size: 34px;
            color: #8c939d;
            img{
                width: 200px;
                height: 200px;
            }
            .upload-hide {
                width: 200px;
                height: 200px;
                display: flex;
                justify-content: center;
                align-items: center;
            }
        }
    }
</style>